package yunquan.wang.base.dao.springdata.audit;

import org.springframework.data.domain.AuditorAware;

import yunquan.wang.base.spring.SpringContextHolder;
import yunquan.wang.base.spring.security.SpringSecurityUtils;
import yunquan.wang.base.spring.security.UserDetail;
import yunquan.wang.modules.base.entity.UserBasic;
import yunquan.wang.modules.base.service.UserBasicService;

/**
 * Dummy implementation of {@link AuditorAware}. It will return the configured
 * {@link AuditableUser} as auditor on every call to
 * {@link #getCurrentAuditor()}. Normally you would access the applications
 * security subsystem to return the current user.
 * 
 * @author Oliver Gierke
 */
public class AuditorAwareImpl implements AuditorAware<UserBasic> {
	private UserBasicService userBasicService = SpringContextHolder
			.getBean(UserBasicService.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.data.domain.AuditorAware#getCurrentAuditor()
	 */
	public UserBasic getCurrentAuditor() {
		UserDetail user = SpringSecurityUtils.getCurrentUser();
		if (user == null) {
			return null;
		}
		return userBasicService.get(user.getUserId());
	}
}
